##第1阶段 打包jar
FROM maven:3.8.2-jdk-8 AS buildapp
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mkdir -p /root/.m2 && \
    echo '<?xml version="1.0" encoding="UTF-8"?>' > /root/.m2/settings.xml && \
    echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"' >> /root/.m2/settings.xml && \
    echo '          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' >> /root/.m2/settings.xml && \
    echo '          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">' >> /root/.m2/settings.xml && \
    echo '  <mirrors>' >> /root/.m2/settings.xml && \
    echo '    <mirror>' >> /root/.m2/settings.xml && \
    echo '      <id>aliyunmaven</id>' >> /root/.m2/settings.xml && \
    echo '      <mirrorOf>*</mirrorOf>' >> /root/.m2/settings.xml && \
    echo '      <name>阿里云公共仓库</name>' >> /root/.m2/settings.xml && \
    echo '      <url>https://maven.aliyun.com/repository/public</url>' >> /root/.m2/settings.xml && \
    echo '    </mirror>' >> /root/.m2/settings.xml && \
    echo '    <mirror>' >> /root/.m2/settings.xml && \
    echo '      <id>aliyunmaven-central</id>' >> /root/.m2/settings.xml && \
    echo '      <mirrorOf>central</mirrorOf>' >> /root/.m2/settings.xml && \
    echo '      <name>阿里云central仓库</name>' >> /root/.m2/settings.xml && \
    echo '      <url>https://maven.aliyun.com/repository/central</url>' >> /root/.m2/settings.xml && \
    echo '    </mirror>' >> /root/.m2/settings.xml && \
    echo '  </mirrors>' >> /root/.m2/settings.xml && \
    echo '</settings>' >> /root/.m2/settings.xml
# 执行Maven打包命令，跳过测试以加快构建速度

RUN mvn clean package '-Dmaven.test.skip=true'

RUN ls -l && pwd && ls -l target && \
     find target -name "*.jar" -type f -exec ls -l {} + | sort -nr -k5 | head -n1 | awk '{print $9}' | xargs -I{} cp {} app.jar


##第2阶段 构建jra 镜像
#只需要一个jre 运行环境就可以
FROM openjdk:17-jdk-alpine
#修改时区，
#RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
LABEL maintainer="baimeidashu89"
#从第一个阶段的jra 复制过来
COPY --from=buildapp /app/app.jar /app.jar

EXPOSE 8080
ENV  JAVA_OPTS="-Xms512m -Xmx2048m -XX:+UseG1GC"
ENV PARAMS="--spring.profiles.active=test"
ENTRYPOINT ["/bin/sh","-c","java -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar $PARAMS"]

